package com.tuniu.agents.common.command;

import com.tuniu.agents.command.CommandConfig;
import com.tuniu.agents.command.CommandLifecycle;
import com.tuniu.agents.common.command.action.DefaultCallAgentAction;
import com.tuniu.agents.common.command.action.DefaultOutputTextAction;
import com.tuniu.agents.common.command.match.StringMatch;
import org.apache.groovy.util.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Component
public class CommandManagerUtil {

    public static CommandManager commandManager;

    @Autowired
    public CommandManagerUtil(CommandManager commandManager) {
        this.commandManager = commandManager;
    }

    /**
     * Save list shortcut command
     *
     * @param conversationId Conversation ID
     * @param commandId      Command ID
     * @param matchName      Match name
     * @param actionName     Action name
     * @param params         Parameters
     */
    public static void saveListCommand(String conversationId, String commandId, String matchName, String actionName, Map<String, Object> params) {
        CommandConfig commandConfig = CommandConfig.builder()
                .lifecycle(CommandLifecycle.ITERATED)
                .id(commandId)
                .priority(10000)
                .matchName(matchName)
                .actionName(actionName)
                .params(params)
                .build();
        commandManager.saveCommandConfig(conversationId, commandConfig);
    }

    public static void saveStringAgentCommand(String conversationId, String commandId, Long Number, String agentId, String message) {
        saveListCommand(conversationId, commandId, "stringMatch", "defaultCallAgentAction", Maps.of(
                DefaultCallAgentAction.PARAM_AGENT_ID, agentId,
                DefaultOutputTextAction.KEY_MESSAGE, message,
                StringMatch.KEY_STRING, List.of(String.valueOf(Number), message)));
    }

    public static void saveStringOutputCommand(String conversationId, String commandId, Long Number, String message) {
        saveListCommand(conversationId, commandId, "stringMatch", "defaultOutputTextAction", Maps.of(
                DefaultOutputTextAction.KEY_MESSAGE, message,
                StringMatch.KEY_STRING, String.valueOf(Number)));
    }

}
